<template>
  <div class="login-container">
    <!-- 登陆盒子 -->
    <div class="login-box">
      <!-- 头像盒子 -->
      <div class="avatar-box">
        <!-- 头像图片 -->
        <img src="../assets/logo.png" alt="" />
      </div>

      <!-- 登陆表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginFrom"
        :rules="loginRules"
        label-width="0px"
        class="login-form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <!-- 表单框前图标 -->
          <el-input
            v-model="loginFrom.username"
            prefix-icon="iconfont icon-people_fill"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 密码  -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginFrom.password"
            prefix-icon="iconfont icon-lock_fill"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="success">登陆</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      //   登陆表单数据绑定对象
      loginFrom: {
        username: "admin",
        password: "123456",
      },
      // 表单验证规则
      loginRules: {
        // 验证账号是否合法
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,max: 10,message: "长度在 5 到 10 个字符",trigger: "blur",
          },
        ],
      },
    };
  },
  methods:{
    // 重置表单事件
    reset(){
      // console.log(this);
      this.$refs.loginFormRef.resetFields();
       this.$message.warning('已重置');
    },
    // 预验证，表单验证是否合法
    success(){
       this.$refs.loginFormRef.validate (async valid=>{
        //  console.log(valid);
        // 验证API服务器是否存在该用户
        if(!valid) return;
        const {data: res}= await this.$http.post("login",  this.loginFrom);
        // console.log(res);
        if(res.meta.status !== 200) return this.$message.error('登陆失败');
        this.$message.success('您已成功登录');
        console.log(res);
        // token只应在当前网站位置期间生效，所以将token保存在sessionStorage中
        window.sessionStorage.setItem('token',res.data.token);
        // 通过编程时导航跳转到后台主页，路由地址是/home
        this.$router.push('/home');
       });
    }
  }
};
</script>

<style lang='less' scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 360px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar-box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  //   表单出超出位置，修改默认
  box-sizing: border-box;
}

.btns {
  display: flex;
  //   具有对齐
  justify-content: flex-end;
}
</style>